package com.miyu.system.utils.system;

import com.miyu.common.utils.web.PropertiesUtil;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * 系统工具
 * @author miyu
 * @date 2022/11/02
 */
public class SystemUtils {

    /**
     * uuid生成
     * @return
     */
    public static   String getUUID(){
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        return uuid;
    }

    /**
     * 根据字符转换MD5可校验数据时候重复
     * @param inStr
     * @return
     */
    public static String MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * 分表处理章节数据
     * 由于章节过多分10张表处理 增加查询效率
     * @return
     */
    public static String [] createTable(){
        String [] rs = new String[10];
        rs[0] = "a";
        rs[1] = "b";
        rs[2] = "c";
        rs[3] = "d";
        rs[4] = "e";
        rs[5] = "f";
        rs[6] = "g";
        rs[7] = "h";
        rs[8] = "i";
        rs[9] = "j";
        return  rs;
    }

    public static void main(String[] args) {
        int s = createTable().length;
        String [] strings = createTable();
        Random rd=new Random();
        System.out.println(strings[rd.nextInt(s)] );
    }

    /**
     * 随机获得一张表
     * @return
     */
    public static String getTable (){
        String [] rds = createTable();
        Random rd=new Random();
        return rds[rd.nextInt(rds.length)];
    }
    /**
     * 判断当前数据库连接类型
     */
    public static String baseDataConnectionType(){
        String str = "sql_server";

        String driver =  PropertiesUtil.getProperty("spring.jpa.database");
        if(driver.indexOf("mysql")>-1) {
            return "mysql";
        }

        return str;
    }

    /**
     * 判断数据库连接类型 mysql 或者smmsql
     * @return
     */
    public static boolean IsMysql(){
        if(baseDataConnectionType().indexOf("sql_server")>-1) {
            return  false;
        }
        return true;
    }
}
